﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;

namespace 第九次作业
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            dataBox.Text = "网页数据暂无";
        }
        /*定义一些全局变量*/
        string webInfo;
        /*爬取数据按钮点击触发效果*/
        private void dataButton_Click(object sender, EventArgs e)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(urlBox.Text) as HttpWebRequest;
                WebResponse response = request.GetResponse();
                Stream resStream = response.GetResponseStream();
                StreamReader sr = new StreamReader(resStream);
                dataBox.Text = webInfo = sr.ReadToEnd();
            }
            catch (Exception)
            {
                MessageBox.Show("输入的地址格式错误！");
            }
        }
        /*提取邮箱电话按钮触发事件*/
        private void emailButton_Click(object sender, EventArgs e)
        {
            try
            {
                //邮箱格式的正则表达式（匹配串和验证串不同）
                string validEm = @"[a-zA-Z0-9]+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*[a-zA-Z0-9]";
                //手机号码格式的正则表达式
                string validPh = @"[1]+[3,4,5,7,8]+\d{9}";
                emailBox.Clear();
                //针对邮箱进行正则匹配
                foreach (Match match in Regex.Matches(webInfo, validEm))
                {
                    emailBox.Text = emailBox.Text + match.ToString() + "\r\n";
                }
                phoneBox.Clear();
                //针对电话进行正则匹配
                foreach (Match match in Regex.Matches(webInfo, validPh))
                {
                    phoneBox.Text = phoneBox.Text  + match.ToString() + "\r\n";
                }
                //空处理
                if (emailBox.Text.Equals(""))
                {
                    emailBox.Text = "当前爬取的页面无邮箱！";
                }
                if (phoneBox.Text.Equals(""))
                {
                    phoneBox.Text = "当前爬取的页面无电话！";
                }
            }
            catch (Exception)
            { }
        }
    }
}